<?php
defined('BASEPATH') OR exit('No direct script access allowed');
?>
<style>
.table-bordered,.table-bordered>thead>tr>th, .table-bordered>tbody>tr>th, .table-bordered>tfoot>tr>th, .table-bordered>thead>tr>td, .table-bordered>tbody>tr>td, .table-bordered>tfoot>tr>td{
    border: 1px solid #cdcdcd;
}
</style>
<div class="row">
    <div class="col-sm-12">
        <div class="box">
        <div class="box-header with-border">
       
        <h2 class="page-header">
        
        </h2>
        
        </div>
        <div class="box-body">

        <?php if($map):?>
            <table class="table table-striped table-hover table-bordered">
                <thead>
                    <tr>
                       <!-- <th>ID</th>-->
                        <th>账号</th>
                        <th>绑定站点</th>
                        <th width="120">操作</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($map as $item):?>
                    <tr shopid="<?php echo $item["id"]; ?>">
                        <!--<td><?php echo $item["id"]; ?></td>-->
                        <td><div><a href="/backend/adword/account/detail/<?php echo $item["id"];?>" style="color:#1a73e8" target="_blank"><?php echo $item["accountdesc"];?></div>
                                    <div style="color: rgba(0,0,0,.54);"><?php echo $item["account"];?></div></td>
                        <td><?php echo $item["siteid"]>0?$sites[$item["siteid"]]["name"]:"尚未绑定";?></td>
                        <td>
                        <span class="btn btn-xs btn-flat btn-success btn-bind" itemid="<?php echo $item["id"];?>"><i class="fa fa-add-o fa-lg"></i>绑定</span>
                        <?php
                        if($item["siteid"]>0):
                        ?>
                        <span class="btn btn-xs btn-flat btn-danger btn-delete" itemid="<?php echo $item["id"];?>">清空</span>
                        <?php
                        endif;
                        ?>
                        </td>
                    </tr>
                    <?php endforeach;?>
                </tbody>
                </table>
        <?php endif;?>

        <?php 
        if(empty($map)){
            echo "<h2 style='text-align:center;padding:50px 0;'>没有查询到数据</h2>";
        }
        ?>

        </div>
    </div>
</div>



<div class="modal fade" id="bindmodal" data-backdrop="false" tabindex="666" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title" id="myModalLabel">广告账号绑定店铺</h4>
            </div>
            <div class="modal-body">
                  
                    <div class="row">
                        <div class="col-sm-2"><label><span style="line-height:30px;">广告账户</span></label></div>
                        <div class="col-sm-5" id="accountholder"></div>
                    </div>

                    <div class="row" style="margin-top:25px;">
                        <div class="col-sm-2"><label><span style="line-height:30px;">选择店铺</span></label></div>
                        
                        <div class="col-sm-5">
                            
                        <div class="input-group">
							<select  class="form-control" id="sites" name="sites" style="width:300px">
							 <?php
								foreach($sites as $sb)
								{
									echo '<option value="'.$sb["id"].'">'.$sb["name"].'</option>';
								}
							 ?>
							
							</select>
                        </div>
                        <input type="hidden" name="bindid" id="bindid">
                       </div>
                    </div>
                    
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button type="button" class="btn btn-primary" id="submitmoify">绑定站点</button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal -->
</div>

<script>
    $(function () {
        $.fn.modal.Constructor.prototype.enforceFocus = function(){};
        $("#sites").select2();
        $(".btn-bind").on("click",function(){
            $("#accountholder").html($(this).parent().parent().find("td:eq(0)").html());
            $("#bindid").val($(this).attr("itemid"));
            $('#bindmodal').modal('show');
        });

        $('#bindmodal').on('shown.bs.modal', function (e) {
            $(this).css('display', 'block');
            var modalHeight=$(window).height() / 2 - $('#bindmodal .modal-dialog').height() / 2;
            $(this).find('.modal-dialog').css({
                'margin-top': modalHeight
            });
        });




        $("span.btn-delete").on("click",function(event){
            var cid=$(this).attr("itemid");
            layer.confirm("确认要清空绑定吗？", { title: "确认" }, function (index) {           
                
                var ii = layer.load(1, {
                    shade: [0.1,'#000'] 
                });
                var postdata={"accid":cid,"siteid":"0"};
                $.ajax({
                    type:"post",
                    url:"/backend/adword/account/ajaxbind",
                    data:postdata,
                    error:function(){
                        layer.msg("内部错误");
                        layer.close(ii);
                    },
                    success:function(result){
                        window.location.href=window.location.href;
                    },
                });
                layer.close(index); 
            });
            
        });

        $("#submitmoify").on("click",function(){
            var accid=$("#bindid").val();
            var siteid=$("#sites").val();
            var postdata={"accid":accid,"siteid":siteid};
            var ii = layer.load(1, {
                shade: [0.1,'#000'] 
            });
            $.ajax({
                type:"post",
                url:"/backend/adword/account/ajaxbind",
                data:postdata,
                error:function(){
                    layer.msg("内部错误");
                    layer.close(ii);
                },
                success:function(result){
                    window.location.href=window.location.href;
                },
            });
        });

    });
</script>
